// pages/community/community.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    topicList:[],
    imageList:[],
    zanNumber:0,
    length:"",
    showDelete:false,
    openid:"",
    star:[],
    type:"全部",
    list:[
      {"txt":"全部","src":""},
      {"txt":"中医","src":""},
      {"txt":"草药","src":""},
      {"txt":"方剂","src":""},
      {"txt":"实验","src":""},
      {"txt":"诊断","src":""},
      {"txt":"求助","src":""},
      {"txt":"答疑","src":""},
      {"txt":"班级","src":""},
    ]
  },

  delete(){
    var that=this
    that.setData({
      showDelete:true
    })
  },
  onShow: function () {
    let that=this
    wx.setNavigationBarTitle({
      title: '学习社区',
    })
    wx.cloud.database().collection("recodeList").get({
      success(res){
        that.setData({
          topicList:res.data,
          openid:wx.getStorageSync('openid')
        })
        console.log(that.data.openid)
      }
    })
    wx.cloud.database().collection("userList").where({openid:wx.getStorageSync('openid')}).get({
      success(res){
        console.log(res.data)
        that.setData({
          star:res.data[0].star
        })
        console.log(that.data.star)
      }
    })
    console.log(that.data.star)
  },
  toContext(e)
  {
    var that=this
    var id=e.currentTarget.dataset.id
    var star=that.data.star
    // star=that.data.userList.star
    console.log(star)
    var i=0
    var len=star.length
    // var topicList=JSON.stringify(that.data.topicList)
    // console.log(topicList)
    // while(i<len)
    // {
    //   if(star[i].id==id)
    //   {
    //     wx.navigateTo({
    //       // topicList='+topicList
    //       url: '../comContext/comContext?id='+id+"&star=true"
    //     })
    //   }
    //   i++
    // }
    wx.navigateTo({
      // topicList='+topicList
      url: '../comContext/comContext?id='+id
    })
  },
  changeItem(e)
  {
    let that=this
    let type=e.currentTarget.dataset.type
    this.setData({
      type:type
    })
    if(that.data.type=="全部")
    {
      wx.cloud.database().collection("recodeList").get({
        success(res){
          console.log(res)
          that.setData({
            topicList:res.data,
          })
        }
      })
    }
    else
    {
      wx.cloud.database().collection("recodeList").where({
        type:this.data.type
      }).get({
        success(res){
          console.log(res)
          that.setData({
            topicList:res.data,
          })
          console.log(that.data.topicList)
        }
      })
    }
  },
  upload(){
    wx.navigateTo({
      url: '../newTopic/newTopic',
    })
  }
})